package liumengbaio.lolzhangyoubao.Activity.Activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.io.IOException;

import liumengbaio.lolzhangyoubao.Activity.Adapter.FindFriendItemAdapter;
import liumengbaio.lolzhangyoubao.Activity.InFo.FindFriendInfo;
import liumengbaio.lolzhangyoubao.R;

public class FindFriendActivity extends AppCompatActivity {
    private ListView find_friend_listView;
    private FindFriendItemAdapter adapter;
    private FindFriendInfo findFriendInfo;
    private int totalHeight = 0;
    private String url = "http://lol.zhangyoubao.com/apis/re" +
            "st/UgcsService/getCompanions?userid=&i_=Lf2he" +
            "/rdpOPtR1PwqdUiuwS64wm6xMV8rBpebf4NEuI=&devic" +
            "eId=Lf2he/rdpOPtR1PwqdUiuwS64wm6xMV8rBpebf4NE" +
            "uI=&t_=1473993998110&p_=26675&v_=400902&a_=lo" +
            "l&pkg_=com.anzogame.lol&d_=android&osv_=17&ch" +
            "a=baiduMartket&u_=&modle_=vivo%20Y15T&api=Ugc" +
            "sService.getCompanions&time=1473993862&game=l" +
            "ol&secretId=AKIDz8krbsJ5yKBZQpn74WFkmLPx3gnPh" +
            "ESA&secretVersion=v1.0&nonce=633005&secretSign" +
            "ature=lP9A/Jokr/S3ippTs6ORW8nJS7c=";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_friend);
        initView();
    }

    private void initView() {
        findViewById(R.id.edit_head_left_title).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        find_friend_listView = (ListView) findViewById(R.id.find_friend_listView);
        getDataFromWeb();
    }

    private void getDataFromWeb() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder()
                        .url(url)
                        .build();
                Call call = client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Request request, IOException e) {
                        Toast.makeText(FindFriendActivity.this, "数据获取错误", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(Response response) throws IOException {
                        String result = response.body().string();
                        Gson gson = new Gson();
                        findFriendInfo = gson.fromJson(result, FindFriendInfo.class);
                        if (findFriendInfo.getData().size() > 0) {
                            handler.sendEmptyMessage(1);
                        } else {
                            handler.sendEmptyMessage(0);
                        }
                    }
                });
            }
        }).start();
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                adapter = new FindFriendItemAdapter(FindFriendActivity.this, findFriendInfo);
                for (int i = 0; i < adapter.getCount();i++){

                    View listViewItem = adapter.getView(i, null, find_friend_listView);

                    listViewItem.measure(0, 0);

                    totalHeight += listViewItem.getMeasuredHeight();

                }

                ViewGroup.LayoutParams params = find_friend_listView.getLayoutParams();

                params.height = totalHeight + (find_friend_listView.getDividerHeight() * (adapter.getCount() - 1));

                find_friend_listView.setLayoutParams(params);
                find_friend_listView.setAdapter(adapter);
            } else {
                Toast.makeText(FindFriendActivity.this, "数据为空", Toast.LENGTH_SHORT).show();
            }
        }
    };
}
